package com.atguigu.ggkt.exception;

import com.atguigu.ggkt.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * FileName:     GlobalExceptionHandler
 * CreateBy:     IntelliJ IDEA
 * Author:       wei
 * Date:         2023-02-15
 * Description :
 */
@ControllerAdvice  //aop
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody //传出json数据
    public Result error(Exception e){
        e.printStackTrace();
        return Result.fail(null).message("执行全局异常处理");

    }

    //特定异常处理ArithmeticException
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody //传出json数据
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail(null).message("执行ArithmeticException异常处理");

    }


    //自定义异常处理ArithmeticException
    @ExceptionHandler(GgktException.class)
    @ResponseBody //传出json数据
    public Result error(GgktException e){
        e.printStackTrace();
        return Result.fail(null).code(e.getCode()).message(e.getMessage());

    }



}
